/**
 * @author 
 * Yannic Remmet			196983
 * Tobias Trabelsi			197112
 * Anna-Katharina Wickert	197122
 */
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedList;
import java.io.*;


public class Compare {

	public static void main(String[] args) {
		int n = Integer.parseInt(args[0]);
		long start;
		//Array
		int[] newArr = new int[] {};
		long[] timeArr = new long[4]; // Saving the time for all operations. 
		//ArrayList
		ArrayList<Integer> arrL = new ArrayList<Integer>();
		long[] timeArrL = new long[4];
		//LinkedList
		LinkedList<Integer> linkedL = new LinkedList<Integer>();
		long[] timeLinkedL = new long[4];
		
		Writer wrtr = null;

		
		//Testing the Array.
		System.out.println("Testing the speed of an Array with n = " + n);
		
		System.out.println("First test: ");
		start = System.currentTimeMillis();
		for (int i = 0; i<n; i++){
			newArr = Arrays.copyOf(newArr, i+1);
			newArr[i] = i+n;
		}
		timeArr[0] = System.currentTimeMillis() - start;
		System.out.println("Time : " + timeArr[0]		);
		
		System.out.println("Second test: ");
		start = System.currentTimeMillis();
		for (int i = 0; i <n; i++){
			newArr = Arrays.copyOf(newArr, n+i+1);
			System.arraycopy(newArr, 0, newArr, 1, n+i);
			newArr[0] = n-i-1;
		}
		timeArr[1] = System.currentTimeMillis() - start;
		System.out.println("Time: " + timeArr[1]);
		
		System.out.println("Third test:" );
		start = System.currentTimeMillis();
		for (int i = 0; i < newArr.length; i++){
			System.out.print(newArr[i] + " ");
		}
		timeArr[2] = System.currentTimeMillis() - start;
		System.out.println();
		System.out.println("Time: " + timeArr[2]);
		
		System.out.println("Fourth test:");
		for (int i = 0; i <n; i++){
			newArr[0] = 0;
			System.arraycopy(newArr, 1, newArr, 0, newArr.length - 1);
		}
		timeArr[3] = System.currentTimeMillis() - start;
		System.out.println("Time: " + timeArr[3]);
		
		// Testing the ArrayList
		System.out.println("First test:");
		start = System.currentTimeMillis();
		for (int i =0; i < n ; i++){
			arrL.add(n+i);
		}
		timeArrL[0] = System.currentTimeMillis() - start;
		System.out.println("Time: " + timeArrL[0]);
		
		System.out.println("Second test:");
		start = System.currentTimeMillis();
		for (int i = 0; i < n; i++){
			arrL.add(0, n-i-1);
		}
		timeArrL[1] = System.currentTimeMillis() - start;
		System.out.println("Time: " + timeArrL[1]);
		
		System.out.println("Third test:");
		start = System.currentTimeMillis();
		for (int i = 0; i < arrL.size(); i++)
			System.out.print(arrL.get(i) + " ");
		timeArrL[2] = System.currentTimeMillis() - start;
		System.out.println("Time: " + timeArrL[2]);
		
		System.out.println("Fourth test:");
		start = System.currentTimeMillis();
		for (int i = 0; i < n; i++)
			arrL.remove(0);
		timeArrL[3]	= System.currentTimeMillis() - start;
		System.out.println("Time: " + timeArrL[3]);
		
		//Testing the LinkedList
		System.out.println("First test:");
		start = System.currentTimeMillis();
		for (int i = 0; i < n; i++)
			linkedL.add(i+n);
		timeLinkedL[0] = System.currentTimeMillis() - start;
		System.out.println("Time: " + timeLinkedL[0]);
		
		System.out.println("Second test:");
		start = System.currentTimeMillis();
		for (int i = 0; i < n; i++){
			linkedL.addFirst(n-i-1);
		}
		timeLinkedL[1] = System.currentTimeMillis() - start;
		System.out.println("Time: " + timeLinkedL[1]);
		
		System.out.println("Third test:");
		start = System.currentTimeMillis();
		for (int i = 0; i < linkedL.size(); i++)
			System.out.print(linkedL.get(i) + " ");
		timeLinkedL[2] = System.currentTimeMillis() - start;
		System.out.println("Time: " + timeLinkedL[2]);
		
		System.out.println("Fourth test:");
		start = System.currentTimeMillis();
		for (int i = 0; i < n; i++)
			linkedL.removeFirst();
		timeLinkedL[3] = System.currentTimeMillis() - start;
		System.out.println("Time: " + timeLinkedL[3]);
		
		try {
			wrtr = new FileWriter("result.txt");
			wrtr.write("\t\t\tArray \tArrayList \tLinkedList \n");
			wrtr.write("add " + n + " el. @end: \t" + Long.toString(timeArr[0]) + '\t' + Long.toString(timeArrL[0]) + "\t\t" + Long.toString(timeLinkedL[0]) + '\n');
			wrtr.write("add " + n + " el. @beg.: \t" + Long.toString(timeArr[1]) + '\t' + Long.toString(timeArrL[1]) + "\t\t" + Long.toString(timeLinkedL[1]) + '\n');
			wrtr.write("Acess over index: \t" + Long.toString(timeArr[2]) + '\t' + Long.toString(timeArrL[2]) + "\t\t" + Long.toString(timeLinkedL[2]) + '\n');
			wrtr.write(n + " rem. @beg.: \t"+ Long.toString(timeArr[3]) + '\t' + Long.toString(timeArrL[3]) + "\t\t" + Long.toString(timeLinkedL[3]) + '\n');
		}
		catch (IOException e) {
			System.err.println("Not able to create the file.");
		}
		finally {
			try { wrtr.close(); } catch ( Exception e ) { } 
		}
			
	}
}
